package com.wyb.service.impl;

import com.wyb.mapper.ArticleMapper;
import com.wyb.mapper.MessageMapper;
import com.wyb.pojo.Article;
import com.wyb.pojo.Liked;
import com.wyb.pojo.Message;
import com.wyb.constant.Result;
import com.wyb.service.LikedService;
import com.wyb.utils.JwtUtil;
import com.wyb.utils.RedisCache;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @BelongsProject: my_blog
 * @BelongsPackage: com.wyb.service.impl
 * @Author: ruge
 * @CreateTime: 2023-02-24  00:26
 * @Description: TODO LikedService的实现类
 * @Version: 1.0
 */
@Service
public class LikedServiceImpl implements LikedService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private MessageMapper messageMapper;
    @Override
    public Result likedOp(int id, int status, String token) {
        //获取用户ID
        String userId;
        try {
            Claims claims = JwtUtil.parseJWT(token);
            userId = claims.getSubject();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(token);
            throw new RuntimeException("token非法");
        }
        //根据状态执行实现对应功能 1 点赞； 2 取消点赞； 3 点踩 ；4 取消点踩
        switch (status){
            case 1:  {
                try {
                    Integer b = articleMapper.likedAdd(id);
                    if(b==0){
                        return new Result(201,"点赞失败",null);
                    }
                    redisCache.add(Liked.ARTICLE_LIKED_KEY+id, userId);
                    if(redisCache.isMemory(Liked.ARTICLE_UNLIKED_KEY+id, userId)){
                        boolean remove = redisCache.remove(Liked.ARTICLE_UNLIKED_KEY + id, userId);
                        if(remove){
                            articleMapper.unlikedSub(id);
                        }
                    }
                    //发送消息
                    Article article = articleMapper.selectById(id);
                    messageMapper.insert(new Message().setLikedUserId(Integer.parseInt(userId)).setTypeId(1).setUserId(article.getUserId()).setTopicId(article.getId()));
                }catch (Exception e){
                    return new Result(201,e.toString(),null);
                }
                return new Result(200, "点赞成功",null);
            }
            case 2:  {
                try {
                    Integer b = articleMapper.likedSub(id);
                    if(b==0){
                        return new Result(201,"取消点赞失败M",null);
                    }
                    boolean remove = redisCache.remove(Liked.ARTICLE_LIKED_KEY + id, userId);
                    if(!remove){
                        return new Result(201,"取消点赞失败R",null);
                    }
                }catch (Exception e){
                    return new Result(201,e.toString(),null);
                }
                return new Result(200, "取消点赞成功",null);
            }
            case 3:  {
                try {
                    Integer b = articleMapper.unlikedAdd(id);
                    if(b==0){
                        return new Result(201,"点踩失败",null);
                    }
                    redisCache.add(Liked.ARTICLE_UNLIKED_KEY+id, userId);
                    if(redisCache.isMemory(Liked.ARTICLE_LIKED_KEY+id, userId)){
                        boolean remove = redisCache.remove(Liked.ARTICLE_LIKED_KEY + id, userId);
                        if(remove){
                            articleMapper.likedSub(id);
                        }
                    }
                }catch (Exception e){
                    return new Result(201,e.toString(),null);
                }
                return new Result(200, "点踩成功",null);
            }
            case 4:  {
                try {
                    Integer b = articleMapper.unlikedSub(id);
                    if(b==0){
                        return new Result(201,"取消点踩失败M",null);
                    }
                    boolean remove = redisCache.remove(Liked.ARTICLE_UNLIKED_KEY + id, userId);
                    if(!remove){
                        return new Result(201,"取消点踩失败R",null);
                    }
                }catch (Exception e){
                    return new Result(201,e.toString(),null);
                }
                return new Result(200, "取消点踩成功",null);
            }


        }

        return new Result(201, "传入参数不正确", null);
    }
}
